package cn.iocoder.yudao.module.pickbook.controller.admin.userreadinghistory;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.pickbook.controller.admin.userreadinghistory.vo.*;
import cn.iocoder.yudao.module.pickbook.dal.dataobject.userreadinghistory.UserReadingHistoryDO;
import cn.iocoder.yudao.module.pickbook.service.userreadinghistory.UserReadingHistoryService;

@Tag(name = "管理后台 - 用户阅读记录")
@RestController
@RequestMapping("/pickbook/user-reading-history")
@Validated
public class UserReadingHistoryController {

    @Resource
    private UserReadingHistoryService userReadingHistoryService;

    @PostMapping("/create")
    @Operation(summary = "创建用户阅读记录")
    @PreAuthorize("@ss.hasPermission('pickbook:user-reading-history:create')")
    public CommonResult<Long> createUserReadingHistory(@Valid @RequestBody UserReadingHistorySaveReqVO createReqVO) {
        return success(userReadingHistoryService.createUserReadingHistory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户阅读记录")
    @PreAuthorize("@ss.hasPermission('pickbook:user-reading-history:update')")
    public CommonResult<Boolean> updateUserReadingHistory(@Valid @RequestBody UserReadingHistorySaveReqVO updateReqVO) {
        userReadingHistoryService.updateUserReadingHistory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户阅读记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pickbook:user-reading-history:delete')")
    public CommonResult<Boolean> deleteUserReadingHistory(@RequestParam("id") Long id) {
        userReadingHistoryService.deleteUserReadingHistory(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除用户阅读记录")
                @PreAuthorize("@ss.hasPermission('pickbook:user-reading-history:delete')")
    public CommonResult<Boolean> deleteUserReadingHistoryList(@RequestParam("ids") List<Long> ids) {
        userReadingHistoryService.deleteUserReadingHistoryListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户阅读记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pickbook:user-reading-history:query')")
    public CommonResult<UserReadingHistoryRespVO> getUserReadingHistory(@RequestParam("id") Long id) {
        UserReadingHistoryDO userReadingHistory = userReadingHistoryService.getUserReadingHistory(id);
        return success(BeanUtils.toBean(userReadingHistory, UserReadingHistoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户阅读记录分页")
    @PreAuthorize("@ss.hasPermission('pickbook:user-reading-history:query')")
    public CommonResult<PageResult<UserReadingHistoryRespVO>> getUserReadingHistoryPage(@Valid UserReadingHistoryPageReqVO pageReqVO) {
        PageResult<UserReadingHistoryDO> pageResult = userReadingHistoryService.getUserReadingHistoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserReadingHistoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户阅读记录 Excel")
    @PreAuthorize("@ss.hasPermission('pickbook:user-reading-history:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserReadingHistoryExcel(@Valid UserReadingHistoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserReadingHistoryDO> list = userReadingHistoryService.getUserReadingHistoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户阅读记录.xls", "数据", UserReadingHistoryRespVO.class,
                        BeanUtils.toBean(list, UserReadingHistoryRespVO.class));
    }

}